
//库函数头文件包含
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>


//函数状态码定义
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int  Status;

//顺序表的存储结构定义
#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10
typedef int ElemType;  //假设线性表中的元素均为整型
typedef struct{
    ElemType* elem;   //存储空间基地址
    int length;       //表中元素的个数
    int listsize;     //表容量大小
}SqList;    //顺序表类型定义
Status ListInsert_Sq(SqList &L, int pos, ElemType e);
Status ListDelete_Sq(SqList &L, int pos, ElemType &e);
int ListLocate_Sq(SqList L, ElemType e);
void ListPrint_Sq(SqList L);

//结构初始化与销毁操作
Status InitList_Sq(SqList &L){
  //初始化L为一个空的有序顺序表
    L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)exit(OVERFLOW);
    L.listsize=LIST_INIT_SIZE;
    L.length=0;
    return OK;
}


int main() {
    SqList L;

    if(InitList_Sq(L)!= OK) {
        printf("InitList_Sq: 初始化失败！！！\n");
        return -1;
    }

    for(int i = 1; i <= 10; ++ i)
        ListInsert_Sq(L, i, i);

    int operationNumber;  //操作次数
    scanf("%d", &operationNumber);

    while(operationNumber != 0) {
        int operationType;  //操作种类
        scanf("%d", & operationType);

        if(operationType == 1) {  //增加操作
            int pos, elem;
            scanf("%d%d", &pos, &elem);
            ListInsert_Sq(L, pos, elem);
        } else if(operationType == 2) {  //删除操作
             int pos; ElemType elem;
             scanf("%d", &pos);
             ListDelete_Sq(L, pos, elem);
             printf("%d\n", elem);
        } else if(operationType == 3) {  //查找定位操作
            ElemType elem;
            scanf("%d", &elem);
            int pos = ListLocate_Sq(L, elem);
            if(pos >= 1 && pos <= L.length)
                printf("%d\n", pos);
            else
                printf("NOT FIND!\n");
        } else if(operationType == 4) {  //输出操作
            ListPrint_Sq(L);
        }
       operationNumber--;
    }
    return 0;
}

//只是读取，不需要带回，带回用引用&
void ListPrint_Sq(SqList L) {
    //length的长度从0 ：1？
    for(int i=0; i<L.length-1; i++) {
        printf("%d ",L.elem[i]);
    }
    printf("%d\n",L.elem[L.length-1]);
}
int ListLocate_Sq(SqList L, ElemType e) {
    for(int i=0; i<L.length; i++) {
        if(L.elem[i] == e)
        return i+1;
    }
    return -1;
}
//插入操作：在顺序表第i个位置插入元素e
//先检查插入位置是否越界，越界则返回error，
//检查存储空间是否足够，存储空间不足要分配空间
//找到插入位置，将其与位置元素从最后一位开始后移，最后将要插入元素放入
//表的长度要加1
//特殊情况：如果表是空表？位序与次序   存储空间分配失败 扩容之后的存储容量要更新   注意L.listsize L.length
 Status ListInsert_Sq(SqList &L, int pos, ElemType e)
{
    if(pos<1||pos>L.length+1)
        return ERROR;
    if(L.length==L.listsize)
    {
        ElemType* insertnum=(ElemType*) realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!insertnum) exit(OVERFLOW); 
        L.elem = insertnum;
        L.listsize+=LISTINCREMENT;
    }
    ElemType *q=L.elem+pos-1;
    for(ElemType* p=L.elem+L.length-1;p>=q;--p)
        *(p+1)=*p;
    *q=e;
    L.length++;
    return OK;
}
Status ListDelete_Sq(SqList &L, int pos, ElemType &e)
{
     if(pos<1||pos>L.length)
        return ERROR;
     ElemType* p=L.elem+pos-1;
     //此处注意不可随便改指针！
     e=*p;
     ElemType* q=L.elem+L.length-1;
     for(++p;p<=q;++p)
     {
         *(p-1)=*p;
     }
     L.length--;
     return OK;
}